package com.baidumap.administrator.happystudy.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.baidumap.administrator.happystudy.base.JsonTools;
import com.baidumap.administrator.happystudy.base.MyListView;
import com.baidumap.administrator.happystudy.base.Utils;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.MyAddressAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/*
 * 常用地址
 * 
 * */
public class MyAddressActivity extends Activity implements OnClickListener {

    @Bind(R.id.goback)
    LinearLayout goback;
    @Bind(R.id.add_address_btn)
    Button addAddressBtn;
    private LinearLayout mGoBackLay;
    private RelativeLayout mCouponRlay;
    private ImageView mAddAddressImg;

    public ImageLoader loader;
    private MyListView mListView;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private MyAddressAdapter mAdapter;
    private static int pageNo = 1;
    private ArrayList<Map<String, Object>> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_my_address);
        ButterKnife.bind(this);

        iniListView();

//
    }

    private void iniListView() {

        // LoadImage初始化
        loader = ImageLoader.getInstance();
        pageNo = 1;
        mListView = (MyListView) findViewById(R.id.list);
        mAdapter = new MyAddressAdapter(MyAddressActivity.this, loader);
        new MyTask().execute("", "" + pageNo);// 线程加载json数据
    }

    @OnClick({R.id.goback, R.id.add_address_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goback:
                this.finish();
                break;
            case R.id.add_address_btn:
                Intent it = new Intent(MyAddressActivity.this,AddAddressActivity.class);
                startActivity(it);
                break;
        }
    }

    class MyTask extends
            AsyncTask<String, Void, ArrayList<Map<String, Object>>> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }

        @Override
        protected ArrayList<Map<String, Object>> doInBackground(
                String... params) {

            ArrayList<Map<String, Object>> mList = null;
            mList = (ArrayList<Map<String, Object>>) JsonTools.listKeyMaps(
                    "list", Utils.HOUSE_JSON);
            return mList;
        }

        protected void onPostExecute(ArrayList<Map<String, Object>> result) {
            // TODO Auto-generated method stub

            list.addAll(result);//

            mAdapter.bindData(list);
            if (pageNo == 1) {

                mListView.setAdapter(mAdapter);
            }
            pageNo++;
            mAdapter.notifyDataSetChanged();
            super.onPostExecute(result);

        }
    }


    //Activity从后台重新回到前台时被调用
    @Override
    protected void onRestart() {
        super.onRestart();
        //UserAddressListPost();
    }


    // -------------------------------------隐藏输入法-----------------------------------------------------
    // 获取点击事件
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

}